package com.example.shop.generator.controller;

import com.example.shop.base.Result;
import com.example.shop.generator.entity.DataBase;
import com.example.shop.generator.entity.Table;
import com.example.shop.generator.service.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by an on 2016/9/23.
 *
 * @author haocj
 */
@Controller
@RequestMapping(value = "/sys/code")
public class GeneratorController {

    @Autowired
    private TableService tableService;

    @Value("${generator.packageName}")
    private String packageName;
    @Value("${generator.author}")
    private String author;

    @RequestMapping(value = "/list")
    public String list(){
        return "sys/generator/generator";
    }

    @RequestMapping(value = "/info")
    @ResponseBody
    public List<DataBase> info(){
        return tableService.selectDataBase();
    }

    @RequestMapping(value = "/generator")
    @ResponseBody
    public Result generator(String tableName) {

        List<Table> table = tableService.findByName(tableName);
        Integer res = new Generating(packageName,author).builder(tableName,table);
        if (res == 0){
            return Result.success("生成成功");
        }else  if (res == -1){
            return Result.error("未查询到字段信息");
        }else if (res == 2){
            return Result.error("系统异常");
        }
       return null;

    }
}
